package com.coin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coin.domain.*;
import com.coin.service.*;
import com.coin.mapper.CoinConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 李力
 * @description 针对表【coin_config(币种配置信息)】的数据库操作Service实现
 * @createDate 2025-06-12 10:12:53
 */
@Service
public class CoinConfigServiceImpl extends ServiceImpl<CoinConfigMapper, CoinConfig>
        implements CoinConfigService {

    @Autowired
    private CoinService coinService;

    /**
     * @Description: 查询币种的配置信息
     * @Author: 李 力
     * @Date: 2025/6/12
     * @Param: 币种的id值也是配置的ID
     * @return: R
     */
    @Override
    public CoinConfig findByCoinId(Long coinId) {
        if (coinId == null) {
            throw new RuntimeException("参数为空");
        }
        return getById(coinId);
    }

    /**
     * @Description: 币种配置的修改操作
     * @Author: 李 力
     * @Date: 2025/6/12
     * @Param:
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateOrSave(CoinConfig coinConfig) {
        if (coinConfig == null) {
            throw new RuntimeException("参数为空");
        }
        Long id = coinConfig.getId();
        Coin coin = coinService.getById(id);
        if (coin == null) {
            throw new RuntimeException("无对应的币种信息");
        }
        coinConfig.setCoinType(coin.getType());
        coinConfig.setName(coin.getName());
        CoinConfig config = getById(coinConfig.getId());
        if (config == null) {
            return save(coinConfig);
        } else {
            return updateById(coinConfig);
        }
    }
}




